package com.earth.config;

import cn.hutool.core.util.StrUtil;
import feign.Logger;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * openfeign的配置
 * @author liaoxiaojin
 *
 */
@Configuration
@Slf4j
public class FeignConfig implements RequestInterceptor {

	@Override
	public void apply(RequestTemplate requestTemplate) {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if(attributes != null) {
			HttpServletRequest request = attributes.getRequest();
			String jwtToken = request.getHeader("token");
			if (StrUtil.isNotEmpty(jwtToken)) {
				// header里面添加jwt-token
				requestTemplate.header("token", jwtToken);
				//log.info("feign调用前设置header:" + requestTemplate.headers());
			}
		}
		
	}

	@Bean
    Logger.Level feignLoggerLevel() {
        //这里记录所有，根据实际情况选择合适的日志level
        return Logger.Level.NONE;
    }
}
